package org.ting.e_commerce.service;

import org.ansj.domain.Term;
import org.ansj.splitWord.analysis.ToAnalysis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.ting.e_commerce.mapper.ProductMapper;
import org.ting.e_commerce.model.Product;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class ProductService {
    @Autowired
    ProductMapper productMapper;
    private static Set<String> stopWords = new HashSet<>();
    private static final String STOP_WORD_PATH = "src/main/resources/static/stopWords.text";
    static {
        //加载暂停词
        try(BufferedReader bufferedReader = new BufferedReader(new FileReader(STOP_WORD_PATH))) {
            while(true) {
                String line = bufferedReader.readLine();
                if(line == null) {
                    //读取完毕
                    break;
                }
                stopWords.add(line);
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public boolean addProduct(Product product) {
        return productMapper.addProduct(product);
    }

    public List<Product> selectByStoreAndStatus(int storeId, String productStatus) {
        return productMapper.selectByStoreAndStatus(storeId, productStatus);
    }

    public List<Product> selectAll(int page, int pageSize) {
        return productMapper.selectAll(page * pageSize, pageSize);
    }

    public boolean updateById(Product product) {
        return productMapper.updateById(product);
    }
    public List<Product> selectByStatus(String productStatus, int page, int pageSize) {
        return productMapper.selectByStatus(productStatus, (page - 1) * pageSize, pageSize);
    }

    public int selectPageCountOfStatus(String productStatus, int pageSize) {
        return (productMapper.selectProductCountOfStatus(productStatus) - 1) / pageSize + 1;
    }
    public boolean deleteById(Integer productId) {
        return productMapper.deleteById(productId);
    }
    public List<Product> selectByStatusAndWords(String productStatus, String words, int page, int pageSize) {
        List<String> wordList = new ArrayList<>();
        List<Term> terms = ToAnalysis.parse(words).getTerms();
        for(Term t : terms) {
            String key = t.getName();
            if(!stopWords.contains(key) && !key.equals(" ")) {
                wordList.add(key);
            }
        }
        return productMapper.selectByStatusAndWords(productStatus, wordList, (page - 1) * pageSize, pageSize);
    }
    public int selectPageCountOfStatusAndWords(String productStatus, String words, int pageSize) {
        List<String> wordList = new ArrayList<>();
        List<Term> terms = ToAnalysis.parse(words).getTerms();
        for(Term t : terms) {
            String key = t.getName();
            if(!stopWords.contains(key) && !key.equals(" ")) {
                wordList.add(key);
            }
        }
        return (productMapper.selectCountOfStatusAndWords(productStatus, wordList) - 1) / pageSize + 1;
    }
}
